import os

from google.appengine.ext import db
from google.appengine.ext.webapp import template

from model import Collection, Picture

from tools import manager

class CollectionManager(manager.Manager):
    """
    """

    def get_collection_key(self, title):
        """
        """
        if title == '':
            title = ' '

        #
        query = Collection.all()
        query.filter('language =', self.language)
        query.filter('title =', title)
        #
        if query.count() == 0:
            query = Collection.all()
            query.filter('language =', self.language)
            query.order('title')

        # get the collection
        collection = query.get()
        if collection:
            return collection.key()

        # try to get the fisrt
        else:
            return None

    def get_default_collection(self):
        # find the default author
        collection_query = Collection().all()
        collection_query.filter('language =', self.language)
        collection_query.filter('title =', ' ')
        # get the default
        collection = collection_query.get()
        # if no default we create one
        if not collection:
            collection_key = self.new()
            collection = Author().get(collection_key)
        #return the default author
        return collection

    def get_menu(self, edit=False):
        """
        """
        # get all collections
        query = Collection.all()
        query.filter('language =', self.language)
        query.order('title')

        # init menu
        menu = list()
        list_collections = list()

        # set the menu content
        for collection in query:
            title = collection.title.strip()
            if title == '':
                if edit:
                    #
                    list_collections.append(collection)
                else:
                    pass
            else:
                #
                list_collections.append(collection)

        # return the menu at the end
        return list_collections

    def show(self, collection_key=None):
        """
        """
        if not collection_key:
            #
            collection_key = self.new()

        # get the menu at first
        menu = self.get_menu()
        #
        collection = Collection().get(collection_key)
        # update max size
        if collection.html_info:
            collection.html_info = collection.html_info.replace('max_size=',
                        'max_size=%s' % self.size.width)

        # set the template
        self.template_values['action'] = 'collection'
        self.template_values['menu'] = menu
        self.template_values['collection'] = collection

        # find the template
        path = os.path.join(os.path.dirname(__file__),
                                '../templates', 'collection', 'index.html')

        # display the collection
        return template.render(path, self.template_values)

    def edit(self, collection_key):
        """
        """
        # get the menu at first
        menu = self.get_menu(edit=True)
        # get the collection
        collection = Collection().get(collection_key)

        # set the template
        self.template_values['menu'] = menu
        self.template_values['collection'] = collection
        self.template_values['page'] = 'collection'
        self.template_values['images'] = self.get_content_images()

        # find the template
        path = os.path.join(os.path.dirname(__file__),
                            '../templates', 'collection', 'edit.html')

        # display the collection
        return template.render(path, self.template_values)

    def new(self):
        """
        """
        # create the new collection
        collection = Collection()

        # requirement
        collection.title = db.Category(' ')

        # set the current language
        collection.language = self.language

        #
        collection.put()

        return collection.key()

    def delete(self, collection_key):
        """
        """
        #
        collection = Collection().get(collection_key)

        # get corresponding pictures
        query = Picture().all()
        query.filter('collection =', collection)

        # delete relation from picture
        for picture in query:
            picture.collection = None
            picture.put()

        # delete the collection
        collection.delete()

    def save(self, collection_key, title, info):
        """
        """
        #
        collection = Collection().get(collection_key)

        # title can t be empty
        title = title.strip()
        if title == '':
            title = ' '

        # set title
        collection.title = db.Category(title)

        # set info
        collection.info = info
        collection.html_info = self.get_html_content(info)

        # save collection
        collection.put()
